$('.datatable-basic').DataTable({
	 "ajax" : {
		"url" : contextpath + "distributorLevel/list?type=1",
		"data" : function(d) {
            var query = $("#query").serializeObject();
            query["size"] = d.length;
            query["page"] = (d.start/d.length);
			return query;
		},
		"dataFilter": function(data){
            var json = jQuery.parseJSON( data );
            json.recordsTotal = json.totalElements;
            json.recordsFiltered = json.totalElements;
            json.data = json.content;
 
            return JSON.stringify( json );
        }
	 },
	 "columns": [
         { "data": "levelName" },
         { "data": "rebateRate" },
         {
            render : function(data,type, row, meta) {
                return $("#btnGroup").html();
            }
        }
     ],
     "drawCallback": function( settings ) {
		 $(".btn-edit").click(function(){
			$("#edit").validate().resetForm();
		    $("#edit").fill($('.datatable-basic').DataTable().row($(this).closest("tr")).data());
		    $(".select").trigger("change");
		    
		    var row = $('.datatable-basic').DataTable().row($(this).closest("tr")).data();
		    $(":input[name='id']").val(row.id);
            $(":input[name='type']").attr("disabled","disabled");
            $(":input[name='auditStatus']").attr("disabled","disabled");
            $(":input[name='status']").attr("disabled","disabled");
		 });
		 
		 /*$(".btn-edit").click(function () {
             var row = $('.datatable-basic').DataTable().row($(this).closest("tr")).data();
             $("#id").val(row.id);
             $("#type").attr("disabled","disabled");
             $("#auditStatus").attr("disabled","disabled");
             $("#status").attr("disabled","disabled");
		 });*/
		 
		 $(".btn-del").click(function(){
			var row = $('.datatable-basic').DataTable().row($(this).closest("tr")).data();
			swal({
				title : "修改状态",
				text : "相关供应商的等级均会被清空， 确定 删除等级 "+ row.levelName +" ?",
				type : "warning",
				showCancelButton : true,
				confirmButtonColor : "#DD6B55",
				confirmButtonText : "确定",
				cancelButtonText: "取消",
				closeOnConfirm : false
			},function() {
				$.get(contextpath + "distributorLevel/delete/" + row.id, function(d){
					$('.datatable-basic').DataTable().draw();
					swal("渠道商等级删除成功!","", "success");
				})
			});
			
		 });
      }
});

$(function(){
	$(".btn-add").click(function(){
		$("#edit").validate().resetForm();
		
         $(":input[name='type']").removeAttr("disabled");
         $(":input[name='auditStatus']").removeAttr("disabled");
         $(":input[name='status']").removeAttr("disabled");
		$("#edit")[0].reset();
	});
	
	$("#updateDistributor").click(function(){
		
	});
	 
	$("#submitEdit").click(function(){
		if($("#edit").valid()){
			$.post(contextpath + "distributorLevel/save", $("#edit").serialize(), function(){
				$('.datatable-basic').DataTable().draw();
				$('.close').click();
				swal("编辑渠道商等级成功","", "success");
			})
		}
	});
	 
	$("#submit").click(function(){
		$('.datatable-basic').DataTable().draw();
	});

	$('select').select2({
	    minimumResultsForSearch: Infinity
	});	
	
})
